package com.ubivelox.uienginesample;

import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLES10;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.ubivelox.uienginesample.CubeRenderer.TextureLoader;

public class UIEngineSampleTest extends Activity implements TextureLoader{

	private GLSurfaceView	mGlSurfaceView;
	private CubeRenderer	mCubeRenderer;	
	private Bitmap			mBitmap;
	private boolean 		mBitmapChanged;
	private ListView		mListView;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mListView	= (ListView)findViewById(R.id.list_view);
		mListView.setScrollingCacheEnabled( false );
		mListView.setAdapter( new MyListAdapter() );
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(UIEngineSampleTest.this, "u touched " + String.valueOf(position), Toast.LENGTH_SHORT).show();
			}
		});
			
		mGlSurfaceView = (GLSurfaceView) findViewById(R.id.glsurface_view);
		mCubeRenderer = new CubeRenderer(this);
		mGlSurfaceView.setRenderer(mCubeRenderer);
    }

	public void CopyDraw(Bitmap bitmap) {
		mBitmap = Bitmap.createScaledBitmap(bitmap, 64, 64, false);		
//		Log.d("xxxxxxxxxxxxxxxxxxxxxxx", String.valueOf(mBitmap.getHeight()));
		mBitmapChanged = true;		
	}
	
	private class MyListAdapter extends BaseAdapter {
		//-------------------------------------------------------------------------------
		public int getCount() { return 20; }
		public long getItemId(int position) { return position; }
		public Object getItem(int position) { return position; }
		//-------------------------------------------------------------------------------
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView item = (TextView)convertView;
			if( item == null ) item = new TextView(UIEngineSampleTest.this );
			item.setText( String.format( "%03d: ABCDEFGHIJKLMNOPQRTU", position ) );
			item.setPadding( 15, 15, 15, 15 );
			return item;
		}
	}

	@Override
	public void loadTexture(GL10 gl) {		
		if(mBitmapChanged == true) {
			GLUtils.texImage2D(GLES10.GL_TEXTURE_2D, 0, mBitmap, 0);
			mBitmapChanged = false;
			}		
	}
}